﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game 
{
	import away3d.cameras.*;
	import away3d.containers.*;
	import away3d.core.math.*;
	import away3d.core.utils.*;
	
	import flash.display.*;
	import flash.events.*;
	
	/**
	 * ...
	 * @author snega
	 */
	public class ObjectViewer extends Sprite
	{
		public static var PI:Number = 3.141592653589793;
		
		protected var keyboardStatus:Array = new Array(256);
		protected var mouseKeyDown:Boolean = false;
		protected var mousePosX:Number, mousePosY:Number;
		
		protected var cameraRotX:Number = 0, cameraRotY:Number = 0;
		protected var cameraDis:Number = 1500;
		protected var cameraTarget:Number3D = new Number3D(0, 0, 0);		
		
		protected var scene:Scene3D;
		protected var camera:Camera3D;
		protected var view:View3D;
		
		public function ObjectViewer(width:Number = 800, height:Number = 600, zoom:Number = 10, focus:Number = 100) 
		{
			this.scene = new Scene3D();
			this.camera = new Camera3D( { zoom:zoom, focus:focus, x:0, y:0, z:this.cameraDis } );
			
			this.camera.lookAt(this.cameraTarget);
			
			this.view = new View3D( { scene:this.scene, camera:this.camera } );
			this.view.x = width / 2;
			this.view.y = height / 2;
			addChild(this.view);
			
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
			this.init3d();
			this.init2d();
		}
		
		protected function init3d():void {
			
		}
		
		protected function init2d():void {
			
		}
		
		public function onMouseDown( event:MouseEvent ):void {
			mouseKeyDown = true;
		}
		
		public function onMouseUp( event:MouseEvent ):void {
			mouseKeyDown = false;
		}
		
		public function onKeyDown( event:KeyboardEvent ):void {
			keyboardStatus[event.keyCode] = true;
		}
		
		public function onKeyUp( event:KeyboardEvent ):void {
			keyboardStatus[event.keyCode] = false;
		}
		
		protected function processFrame():void {
			// Process any movement or animation here.
		}
		
		protected function onEnterFrame( ThisEvent:Event ):void {
			var offsetX:Number, offsetY:Number;
			
			if (keyboardStatus[90] && mouseKeyDown) {
				offsetX = mouseX - mousePosX;
				offsetY= mouseY - mousePosY;
				mousePosX = mouseX;
				mousePosY = mouseY;

				cameraDis += offsetX;
				cameraDis += offsetY;
				
				this.camera.y = cameraDis * Math.sin(cameraRotY / 64);
				this.camera.x = cameraDis * Math.sin(cameraRotX / 64) * Math.cos(cameraRotY / 64);
				this.camera.z = cameraDis * Math.cos(cameraRotX / 64) * Math.cos(cameraRotY / 64);
			} else if (mouseKeyDown) {
				offsetX = mouseX - mousePosX;
				offsetY = mouseY - mousePosY;
				mousePosX = mouseX;
				mousePosY = mouseY;
				
				cameraRotX += offsetX;
				cameraRotY += offsetY;
				
				if (cameraRotY > PI * 64 / 2) cameraRotY = PI * 64 / 2;
				if (cameraRotY < 0) cameraRotY = 0;
				
				this.camera.y = cameraDis * Math.sin(cameraRotY / 64);
				this.camera.x = cameraDis * Math.sin(cameraRotX / 64) * Math.cos(cameraRotY / 64);
				this.camera.z = cameraDis * Math.cos(cameraRotX / 64) * Math.cos(cameraRotY / 64);
				
				this.camera.lookAt(this.cameraTarget);
			} else {
				mousePosX = mouseX;
				mousePosY = mouseY;
			}
			
			processFrame();
			
			if (keyboardStatus[87] != true)
			{
				this.view.render();
			}
		}
		
	}
	
}